package com.itstyle.seckill.topic.ai;

/**
 * @Auther: liuwenxiang
 * @Date: 2019/11/20 11:10
 * @Description: 折半查找（有序）
 */
public class BinarySearch {

    public  static  int binarySearch(int[] arr,int num){
        int low=1;
        int high = arr.length;
        while (low<=high){
            int mid=(low+high)/2;
            if(arr[mid]<num){
                low =mid+1;
            }else if(arr[mid]>num){
                high=mid-1;
            }else{
               return mid;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int arr[]={2,4,6,7,8,9,34,56,78};
        int num=34;
        int num1=35;
        System.out.println(binarySearch(arr,num));
        System.out.println(binarySearch(arr,num1));
    }


}
